package com.toman.toolbar;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.ColorInt;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.toman.MyApplication;
import com.toman.R;
import com.toman.Util.PermissonUtils;
import com.toman.Util.SPUtils;
import com.githang.statusbar.StatusBarCompat;

import butterknife.ButterKnife;


/**
 * @Description:
 * @Author:邹苏隆
 * @Since:2016/12/16 10:54
 */
public abstract class ToolBarChartActivity extends AppCompatActivity {
    private ToolBarHelper mToolBarHelper;
    public Toolbar toolbar;
    private TextView tvTitle;
    public Handler mHandler;
    public int mLayoutResID;
    public MyApplication myApplication;
    public String token;
   public String msg;
    protected boolean Landscape = false;
public String id;
    public   String companyId;
    public   String mCompanyId;

    public Animation slide_left_to_left,slide_right_to_left,slide_left_to_right,slide_left_to_left_in;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.colorPrimary), false);
    }

    @Override
    public void setContentView(int layoutResID) {

        mToolBarHelper = new ToolBarHelper(this, layoutResID);
//                mToolBarHelper.setToolbarTitle("a");
        toolbar = mToolBarHelper.getToolBar();

        setContentView(mToolBarHelper.getContentView());
        ButterKnife.bind(this);

        /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolbar);
        /*自定义的一些操作*/
        onCreateCustomToolBar(toolbar);
        mLayoutResID = layoutResID;
        myApplication = (MyApplication) getApplication();
        token = (String) SPUtils.get(getApplicationContext(),"token","");
         slide_left_to_left = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_left_to_left);
         slide_right_to_left = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_right_to_left);
         slide_left_to_right = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_left_to_right);
         slide_left_to_left_in = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_left_to_left_in);
//        mToolBarHelper.setToolbarTitle("a");
        if (SPUtils.contains(getApplicationContext(),"cpy_id_now")){
            companyId= (String) SPUtils.get(getApplicationContext(),"cpy_id_now","");
        }
        else
        {
                companyId= (String) SPUtils.get(getApplicationContext(),"cpy_id","");

        }
        mCompanyId=(String) SPUtils.get(getApplicationContext(),"cpy_id","");
        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    }

    public void onCreateCustomToolBar(Toolbar toolbar) {
        tvTitle = (TextView) findViewById(R.id.tv_toolbar_title);
        toolbar.setContentInsetsRelative(0, 0);
        toolbar.inflateMenu(R.menu.toolbar_save);

    }

    @Override
    protected void onResume() {
        /**
         * 设置为横屏
         */
        if (Landscape) {
            if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        } else {
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        }
        super.onResume();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onPause() {
        super.onPause();
    }



    public void initToolBar(String text, @ColorInt int colorId) {
        mToolBarHelper.setTvTitle(text);
        mToolBarHelper.setToolBarColor(colorId);
        StatusBarCompat.setStatusBarColor(this, colorId, false);


        super.setTitle("");
        toolbar = mToolBarHelper.getToolBar();
        if (colorId==getResources().getColor(R.color.white)){
            setArrowBlack();

            mToolBarHelper.setTvColor(getResources().getColor(R.color.black));

            StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.white), true);

        }
        else {
        }
  /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolbar);

    }
    public void initToolBar(String text) {
        mToolBarHelper.setTvTitle(text);


        super.setTitle("");
        toolbar = mToolBarHelper.getToolBar();

  /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolbar);

    }


    public void isLandscape() {
        Landscape = true;
    }

    public void setTvTitle(String text) {
        mToolBarHelper.setTvTitle(text);
    }
    public void setArrowBlack(){
        toolbar.setNavigationIcon(R.mipmap.back_black);
    }


}